package com.whfc.ms.api.fmam.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.PageData;
import com.whfc.common.util.RandomUtil;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fmam.entity.dto.FmamPlanDTO;
import com.whfc.fmam.entity.dto.FmamPlanListCountDTO;
import com.whfc.fmam.entity.qo.FmamPlanAddQO;
import com.whfc.fmam.entity.qo.FmamPlanCheckQO;
import com.whfc.fmam.entity.qo.FmamPlanEditQO;
import com.whfc.fmam.service.FmamPlanService;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fmam.service.MatPlanService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author: ；likang
 * @Description: 物资计划审批
 * @Version：1.0
 */
@Service
public class MatPlanServiceImpl implements MatPlanService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 全部计划
     */
    private static final Integer ALL_PLAN = 0;

    /**
     * 我提交的
     */
    private static final Integer MY_COMMIT_PLAN = 1;

    /**
     * 代我处理的
     */
    private static final Integer MY_HANDLE_PLAN = 2;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;

    @DubboReference(interfaceClass = FmamPlanService.class, version = "1.0.0")
    private FmamPlanService fmamPlanService;

    @Override
    public PageData<FmamPlanDTO> matPlanList(SysUser user, Integer deptId, Integer pageNum, Integer pageSize, Integer type, String states, Date startDate, Date endDate) {
        // 查找类型  0-全部  1-我提交的  2-待处理
        List<Integer> stateList = parseState(states);
        Integer userId = null;
        //我提交的
        if (MY_COMMIT_PLAN.equals(type)) {
            userId = user.getId();
        }
        return fmamPlanService.list(userId, deptId, pageNum, pageSize, stateList, startDate, endDate);
    }

    @Override
    public FmamPlanListCountDTO matPlanListCount(SysUser user, Integer deptId, String states, Date startDate, Date endDate) {
        return fmamPlanService.planListCount(user.getId(), deptId, startDate, endDate);
    }

    @Override
    public FmamPlanDTO matPlanDetail(SysUser user, Integer planId) {
        logger.info("查询材料计划详情,planId:{}", planId);
        FmamPlanDTO fmamPlan = fmamPlanService.detail(planId);
        return fmamPlan;
    }

    @Override
    public void addMatPlan(SysUser user, FmamPlanAddQO request) {
        logger.info("新增材料计划申请,FmamPlanAddQO:{}", request.toString());
        //补全制单用户信息
        request.setUserId(user.getId());
        request.setUserName(user.getNickname());
        fmamPlanService.add(request);
    }

    @Override
    public void editMatPlan(SysUser user, FmamPlanEditQO request) {
        logger.info("重新填报材料计划申请,FmamPlanEditQO:{}", request.toString());
        //补全制单用户信息
        request.setUserId(user.getId());
        request.setUserName(user.getNickname());
        fmamPlanService.edit(request);
    }

    @Override
    public void checkMatPlan(SysUser user, FmamPlanCheckQO request) {
        logger.info("材料计划审批,FmamPlanCheckQO:{}", request.toString());
        //补全用户信息
        request.setOpUserId(user.getId());
        request.setOpUserName(user.getUsername());
        fmamPlanService.checkPlan(request);
    }

    @Override
    public void check1(SysUser user, FmamPlanCheckQO request) {
        request.setOpUserId(user.getId());
        request.setOpUserName(user.getUsername());
        fmamPlanService.check1(request);
    }

    @Override
    public void check2(SysUser user, FmamPlanCheckQO request) {
        request.setOpUserId(user.getId());
        request.setOpUserName(user.getUsername());
        fmamPlanService.check2(request);
    }

    @Override
    public void check3(SysUser user, FmamPlanCheckQO request) {
        request.setOpUserId(user.getId());
        request.setOpUserName(user.getUsername());
        fmamPlanService.check3(request);
    }

    @Override
    public void delMatPlan(Integer planId) {
        logger.info("删除材料计划,planId:{}", planId);
        fmamPlanService.del(planId);
    }

    @Override
    public OssPathDTO uploadFile(MultipartFile file) throws IOException {
        logger.info("审批材料附件上传,file:{}", file.toString());
        String fileName = file.getOriginalFilename();
        String suffix = "";
        if (StringUtils.isNotBlank(fileName) && fileName.contains(".")) {
            suffix = fileName.substring(fileName.lastIndexOf("."));
        }
        String newName = RandomUtil.getRandomFileName() + suffix;
        InputStream inputStream = file.getInputStream();
        String path = filePathConfig.getFilePath("fmam/mat/plan", newName);
        String upload = fileHandler.upload(path, inputStream);
        OssPathDTO data = new OssPathDTO();
        data.setPath(upload);
        return data;
    }

    /**
     * states处理
     *
     * @param states 状态
     * @return state 集合
     */
    private List<Integer> parseState(String states) {
        List<Integer> stateList = null;
        if (StringUtils.isNotBlank(states)) {
            String[] stateArray = states.split(",");
            if (stateArray.length > 0) {
                stateList = Arrays.stream(stateArray).map(Integer::valueOf).collect(Collectors.toList());
            }
        }
        return stateList;
    }
}
